package com.wd.module_list.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.wd.mylibrary_base.bean.FindLeaderBoardBean
import com.wd.mylibrary_list.R



class NewcomerTopAdapter(
    val context: Context,
    val list: MutableList<FindLeaderBoardBean.Result.NewcomerTop>
) : PagerAdapter() {
    override fun getCount(): Int {
        return list.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(context).inflate(R.layout.rank_list_item, null)
        val listBean = list.get(position)
        val findViewById = view.findViewById<ImageView>(R.id.ranking_user_pic_id)
        Glide.with(context)
            .load(listBean.headPic)
            .circleCrop()
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(findViewById)
        //主播名称
        val name = view.findViewById<TextView>(R.id.ranking_user_name_id)
        name.setText(list.get(position).nickName)
        //性别图标
        val sex = view.findViewById<ImageView>(R.id.ranking_user_gender_id)
        if (list.get(position).sex == 1) {
            sex.setImageResource(R.mipmap.man)
        } else {
            sex.setImageResource(R.mipmap.man)
        }
        //排名 奖牌显示
        val rank = view.findViewById<ImageView>(R.id.ranking_user_rank_id)
        //排名 红色数字背景显示
        val rankTv = view.findViewById<TextView>(R.id.ranking_user_rank_tv_id)
        if (list.get(position).rank == 1) {
            rank.setImageResource(R.mipmap.medal1)
            rankTv.visibility = View.GONE
        } else if (list.get(position).rank == 2) {
            rank.setImageResource(R.mipmap.medal2)
            rankTv.visibility = View.GONE
        } else if (list.get(position).rank == 3) {
            rank.setImageResource(R.mipmap.medal3)
            rankTv.visibility = View.GONE
        } else {
            rank.visibility = View.GONE
            rankTv.visibility = View.VISIBLE
            rankTv.setText("${list.get(position).rank}")
        }
        //粉丝数量
        val fanSum = view.findViewById<TextView>(R.id.ranking_user_fanSum_id)
        fanSum.setText("粉丝数：${list.get(position).fanSum}")
        //主播背景
        val personalImage = view.findViewById<ImageView>(R.id.ranking_user_personalImage_id)
       Glide.with(context).load(list.get(position).headPic)
           .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
           .placeholder(R.mipmap.ic_launcher)
           .error(R.mipmap.ic_launcher)
           .into(personalImage)

        //直播的状态 左上角小红标
        val buff = view.findViewById<TextView>(R.id.ranking_user_buff_pic_id)
        //直播状态 中间的直播中白色边框
        val buffLive = view.findViewById<TextView>(R.id.ranking_user_live_buff_id)
        if (list.get(position).liveStatus == 1) {
            buff.setText("直播中")
            buffLive.visibility = View.VISIBLE
        } else {
            buff.setText("录播")
            buffLive.visibility = View.GONE
        }
        //直播间名称
        val title = view.findViewById<TextView>(R.id.ranking_user_title_id)
        title.setText(list.get(position).liveVideoTitle)
        //直播间时间
        val time = view.findViewById<TextView>(R.id.ranking_user_time_id)
        time.setText(list.get(position).liveVideoTime)
        //直播间 播放总时间
        val duration = view.findViewById<TextView>(R.id.ranking_user_duration_id)
        duration.setText(list.get(position).liveVideoDuration)
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//        super.destroyItem(container, position, `object`)
        container.removeView(`object` as View)
    }
}